<template>
	<view class="goods-view">
		<view class="tab-box">
			<figure v-for="(item,index) in typeList.mySortList" :key="index" :class="typeId==item.id?'active-tab':''"
				@tap="handleType(item)">
				<image :src="item.sortIoc" mode="aspectFit"></image>
				<figcaption>
					{{item.sortName}}
				</figcaption>
			</figure>
		</view>
		<scroll-view class="goods-ul" scroll-y="true" lower-threshold="50" @scrolltolower="scrolltolower">
			<view class="goods-item" v-for="(item,index) in goodsList" :key="index" @tap="toDetail(item)">
				<view class="img-box">
					<image class="goods-img" :src="item.pic" mode="aspectFit"></image>
				</view>
				<view class="right">
					<h2>{{item.productName}}<span>{{item.tag}}</span></h2>
					<p>{{item.introduce}}</p>
					<view class="add-cart">
						<view class="price">
							<b>￥</b>
							<span>{{item.price}}</span>
							<span class="ori-price">￥{{item.originalPrice}}</span>
						</view>
						<image :src="imgUrl+'cart-icon.png'" mode=""></image>
					</view>
				</view>
			</view>
			<u-loadmore loadmoreText="正在全力加载更多..." dashed line fontSize="21rpx" nomore-text="没有更多啦!" :status="moreStatus" />
		</scroll-view>
	</view>
</template>

<script>
	import {
		getTypeList,
		getProductList
	} from '@/api/api.js';
	export default {
		data() {
			return {
				imgUrl: this.$imgUrl,
				dadId: null,
				typeId: null,
				typeList: {
					sortName: "",
					mySortList: []
				},
				page: 1,
				total: 0,
				moreStatus: 'nomore',
				goodsList: [],
			}
		},
		onLoad(op) {
			console.log(op);
			this.dadId = op.dadId
			this.getHomeTypeList();

		},
		methods: {

			//获取首页一级分类列表
			getHomeTypeList() {
				const params = {
					type: 1,
					dadId: this.dadId
				}
				getTypeList({
					params
				}).then(res => {
					if (res.code == 200) {
						this.typeList = res.data[0];
						uni.setNavigationBarTitle({
							title: this.typeList.sortName
						})
						if (this.typeList.mySortList.length > 0) {
							this.typeId = this.typeList.mySortList[0].id;
							this.getList()
						}
					}
				})
			},

			// 获取商品列表
			getProductList(params) {
				getProductList(params).then(res => {
					console.log(res);
					this.goodsList = [...this.goodsList, ...res.rows];
					this.total = res.total;
				}).finally(() => {
					uni.hideLoading()
				})
			},

			getList() {
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				this.getProductList({
					pageNum: this.page,
					pageSize: 10,
					sortId: this.typeId
				})
			},


			handleType(item) {
				this.typeId = item.id;
				this.goodsList = [];
				this.page = 1
				this.getList()
			},
			// 滚动加载
			scrolltolower() {
				if (this.total === this.goodsList.length) {
					this.moreStatus = 'nomore'
				} else {
					this.page += 1;
					this.moreStatus = 'loadmore'
					this.getList()
				}
			},
			toDetail(item) {
				uni.navigateTo({
					url: `/package/goodsDetail/goodsDetail?id=${item.id}`
				})
			}
		}
	}
</script>

<style lang="scss">
	.goods-view {
		width: 100%;
		height: 100vh;
		background-color: #f5f5f5;
		display: flex;
		flex-direction: column;

		.tab-box {
			width: 100%;
			display: flex;
			overflow: auto;
			background-color: #fff;
			border-top: 1rpx solid #eee;
			padding: 20rpx 20rpx 0;
			box-sizing: border-box;
			margin-bottom: 27rpx;

			figure {

				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 50rpx;
				padding-bottom: 20rpx;
				box-sizing: border-box;
				position: relative;

				image {
					width: 93rpx;
					height: 93rpx;
					margin-bottom: 22rpx;
				}

				figcaption {
					width: 120rpx;
					text-align: center;
					font-size: 24rpx;
					color: #000000;
					font-family: 'hm-os-med';
				}
			}

			.active-tab {
				transition: all linear .5s;

				&::after {
					content: '';
					position: absolute;
					bottom: 0;
					left: 50%;
					transform: translateX(-50%);
					width: 39rpx;
					height: 8rpx;
					background-color: #8551EB;
					border-radius: 4rpx;
				}
			}


		}

		.goods-ul {
			flex: 1;
			background-color: #fff;
			padding: 40rpx 33rpx;
			box-sizing: border-box;

			.goods-item {
				display: flex;
				padding-bottom: 10rpx;
				border-bottom: 2rpx dashed #ddd;
				margin-bottom: 20rpx;

				.img-box {
					width: 175rpx;
					height: 175rpx;
					margin-right: 20rpx;
					background-color: #f7f7f7;
					display: flex;
					justify-content: center;
					align-items: center;
					border-radius: 17rpx;

					.goods-img {
						max-width: 80%;
					}
				}

				.right {
					flex: 1;

					h2 {
						font-size: 28rpx;
						color: #000000;
						font-family: 'biaoti';
						display: flex;
						align-items: center;

						span {
							margin-left: 8rpx;
							padding: 4rpx 8rpx;
							box-sizing: border-box;
							color: #fff;
							font-size: 20rpx;
							font-family: 'hm-os-text';
							background-color: #8551EB;
							border-radius: 8rpx;
						}
					}

					p {
						font-size: 21rpx;
						color: #000000;
						font-family: 'hm-os-med';
						margin: 16rpx 0 51rpx;
					}

					.add-cart {
						display: flex;
						justify-content: space-between;
						align-items: center;

						.price {
							display: flex;
							align-items: center;

							b {
								font-size: 22rpx;
								color: #F21331;
								font-family: 'hm-os-label';
							}

							span {
								font-size: 40rpx;
								color: #F21331;
								font-family: 'biaoti';
								transform: translateY(-5rpx);
							}

							.ori-price {
								text-decoration: line-through;
								font-size: 24rpx;
								color: #666;
								font-family: 'hm-os-text';
								margin-left: 8rpx;
							}
						}

						image {
							width: 44rpx;
							height: 44rpx;
						}
					}
				}
			}
		}
	}
</style>